package com.srwl.mytx.activity;

import android.os.Bundle;
import android.view.View;

import androidx.fragment.app.FragmentTransaction;

import com.srwl.easeui.widget.MyTitleBar;
import com.srwl.mytx.R;
import com.srwl.mytx.fragment.BuyOrderRecordFragment;
import com.srwl.mytx.fragment.SellOrderRecordFragment;

public class OrderRecordActivity extends BaseActivity {
    private MyTitleBar titleBar;
    private BuyOrderRecordFragment buyOrderRecordFragment;
    private SellOrderRecordFragment sellOrderRecordFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_record);
        titleBar = findViewById(R.id.title_bar);
        titleBar.setRightTitle("切换");
        showBuyOrderFragment();
        titleBar.setRightLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (buyOrderRecordFragment == null || buyOrderRecordFragment.isHidden()) {
                    showBuyOrderFragment();

                } else {
                    showSellOrderFragment();
                }
            }
        });
    }


    private void showBuyOrderFragment() {
        if (buyOrderRecordFragment == null) {
            buyOrderRecordFragment = BuyOrderRecordFragment.newInstance();
        } else if (!buyOrderRecordFragment.isHidden()) {
            return;
        }
        titleBar.setTitle("买入记录");
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        if (sellOrderRecordFragment != null && !sellOrderRecordFragment.isHidden()) {
            fragmentTransaction.hide(sellOrderRecordFragment);
        }

        if (!buyOrderRecordFragment.isAdded()) {
            fragmentTransaction.add(R.id.record_fragment_container, buyOrderRecordFragment);
        }
        fragmentTransaction.show(buyOrderRecordFragment).commit();

    }

    private void showSellOrderFragment() {
        if (sellOrderRecordFragment == null) {
            sellOrderRecordFragment = SellOrderRecordFragment.newInstance();
        } else if (!sellOrderRecordFragment.isHidden()) {
            return;
        }
        titleBar.setTitle("卖出记录");

        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        if (buyOrderRecordFragment != null && !buyOrderRecordFragment.isHidden()) {
            fragmentTransaction.hide(buyOrderRecordFragment);
        }
        if (!sellOrderRecordFragment.isAdded()) {
            fragmentTransaction.add(R.id.record_fragment_container, sellOrderRecordFragment);
        }

        fragmentTransaction.show(sellOrderRecordFragment).commit();

    }

}
